package com.ibeeking.found.content.b.rest.service.impl;


import cn.binarywang.wx.miniapp.api.WxMaLiveGoodsService;
import cn.binarywang.wx.miniapp.api.WxMaLiveService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.live.*;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.content.b.rest.service.IWxBroadcastService;
import com.ibeeking.found.content.b.rest.service.IWxOpenService;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 禁止在此类加其他业务方法
 *
 * @ClassName WxBroadcastServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-30 10:29
 **/
@Service
public class WxBroadcastServiceImpl implements IWxBroadcastService {
    private static final Logger log = LoggerFactory.getLogger(WxBroadcastServiceImpl.class);

//    @Resource
//    private IWxOpenService openService;

    @Resource
    private IWxOpenService wxOpenServiceImpl;
    /**
     * 创建直播间
     *
     * @return WxBroadcastVO
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxMaCreateRoomResult createBroadcastRoom(WxMaLiveRoomInfo wxMaLiveRoomInfo) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        return wxMaLiveService.createRoom(wxMaLiveRoomInfo);
    }

    /**
     * 删除直播间
     *
     * @return WxBroadcastVO
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBroadcastRoom(Integer roomId) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        return wxMaLiveService.deleteRoom(roomId);
    }

    /**
     * 获取直播间列表
     *
     * @return WxBroadcastVO
     */
    @SneakyThrows
    @Override
    public List<WxMaLiveResult.RoomInfo> getLiveRoomList() {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        List<WxMaLiveResult.RoomInfo> liveRoomList = wxMaLiveService.getLiveInfos();
        if (Collections3.isEmpty(liveRoomList)) {
            throw new BusinessException("无法获取直播间列表");
        }

        return null;
    }

    /**
     * 获取直播间信息
     *
     * @return WxBroadcastRoomInfoVO
     */
    @SneakyThrows
    @Override
    public WxMaLiveResult.RoomInfo getLiveRoomInfo(Integer roomId) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();

        List<WxMaLiveResult.RoomInfo> liveRoomList = wxMaLiveService.getLiveInfos();
        if (Collections3.isEmpty(liveRoomList)) {
            throw new BusinessException("无法获取直播间列表");
        }
        Optional<WxMaLiveResult.RoomInfo> liveRoom = liveRoomList.stream().filter(item -> item.getRoomId().equals(roomId)).findFirst();

        return liveRoom.get();
    }

    /**
     * 获取直播间列表
     *
     * @param startSection 起始拉取视频，0表示从第一个视频片段开始拉取
     * @param limit        每次拉取的数量
     * @return WxBroadcastVO
     * @parm roomId 直播间ID
     */
    @SneakyThrows
    @Override
    public List<WxMaLiveResult.LiveReplay> getLiveRoomReplay(Integer roomId, Integer startSection, Integer limit) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();

        WxMaLiveResult wxMaLiveResult = wxMaLiveService.getLiveReplay(roomId, startSection, limit);
        if (wxMaLiveResult == null) {
            throw new BusinessException("无法获取直播间列表");
        }
        List<WxMaLiveResult.LiveReplay> liveReplayList = wxMaLiveResult.getLiveReplay();
        if (Collections3.isEmpty(liveReplayList)) {
            throw new BusinessException("当前无直播回放记录");
        }
        return liveReplayList;
    }

    /**
     * 导入直播间商品
     *
     * @param goodsIds 数组列表，可传入多个，里面填写 商品 ID
     * @return WxBroadcastVO
     * @parm roomId 直播间ID
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addGoodsToLiveRoom(Integer roomId, List<Integer> goodsIds) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        if (Collections3.isEmpty(goodsIds)) {
            throw new BusinessException("商品ID不能为空");
        }
        return wxMaLiveService.addGoodsToRoom(roomId, goodsIds);
    }

    /**
     * 获取直播间推流地址URL
     *
     * @return WxBroadcastVO
     * @parm roomId 直播间ID
     */
    @SneakyThrows
    @Override
    public String getLiveRoomPushStreamUrl(Integer roomId) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        return wxMaLiveService.getPushUrl(roomId);
    }

    /**
     * 获取直播间分享二维码地址URL
     *
     * @return WxBroadcastVO
     * @parm roomId 直播间ID
     * @parm params 可生成带参二维码
     */
    @SneakyThrows
    @Override
    public String getLiveRoomShareQrcodeUrl(Integer roomId, String params) {
        WxMaLiveService wxMaLiveService = getWxMaLiveService();
        WxMaLiveSharedCode wxMaLiveSharedCode = wxMaLiveService.getSharedCode(roomId, params);
        return wxMaLiveSharedCode.toString();
    }

    /**
     * 获取直播服务实例
     *
     * @return WxMaLiveService
     */
    public WxMaLiveService getWxMaLiveService() {
        Long tenantId = UserUtils.getTenantID();
        WxMaService wxMaService = wxOpenServiceImpl.getWxOpenMaService(tenantId);
        return wxMaService.getLiveService();
    }

    /**
     * 获取直播商品服务实例
     *
     * @return WxMaLiveGoodsService
     */
    public WxMaLiveGoodsService getWxMaLiveGoodsService() {
        Long tenantId = UserUtils.getTenantID();
        WxMaService wxMaService = wxOpenServiceImpl.getWxOpenMaService(tenantId);
        return wxMaService.getLiveGoodsService();
    }


    /**
     * ==================================================================================
     * 直播间商品信息维护
     * =================================================================================
     **/


    /**
     * 查询直播间商品
     *
     * @param pageNo    分页条数起点
     * @param pageCount 分页大小，默认30，不超过100
     * @param status    商品状态，0：未审核。1：审核中，2：审核通过，3：审核驳回
     * @return BroadcastGoodsVO
     */
    @SneakyThrows
    @Override
    public List<WxMaLiveResult.Goods> getGoodsList(Integer pageNo, Integer pageCount, Integer status) {
        WxMaLiveGoodsService wxMaLiveGoodsService = getWxMaLiveGoodsService();

        WxMaLiveResult wxMaLiveResult = wxMaLiveGoodsService.getApprovedGoods(pageNo, pageCount, status);
        if (null == wxMaLiveResult) {
            throw new BusinessException("获取商品列表失败");
        }
        return wxMaLiveResult.getGoods();
    }

    /**
     * 查询商品信息
     *
     * @param goodsId 商品ID
     * @return BroadcastGoodsVO
     */
    @SneakyThrows
    @Override
    public WxMaLiveResult.Goods getGoodsInfo(Integer goodsId) {
        WxMaLiveGoodsService wxMaLiveGoodsService = getWxMaLiveGoodsService();
        List goodsIds = new ArrayList();
        goodsIds.add(goodsId);
        WxMaLiveResult wxMaLiveResult = wxMaLiveGoodsService.getGoodsWareHouse(goodsIds);
        if (null == wxMaLiveResult) {
            throw new BusinessException("获取商品列表失败");
        }
        if (Collections3.isEmpty(wxMaLiveResult.getGoods())) {
            return null;
        }
        return wxMaLiveResult.getGoods().get(0);
    }

    /**
     * 添加直播间商品
     *
     * @return WxMaLiveResult
     * @parm broadcastGoodsVO 商品信息
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxMaLiveResult addGoods(WxMaLiveGoodInfo wxMaLiveGoodInfo) {
        WxMaLiveGoodsService wxMaLiveGoodsService = getWxMaLiveGoodsService();
        return wxMaLiveGoodsService.addGoods(wxMaLiveGoodInfo);

    }

    /**
     * 删除直播间商品
     *
     * @return WxMaLiveService
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delGoods(Integer goodsId) {
        WxMaLiveGoodsService wxMaLiveGoodsService = getWxMaLiveGoodsService();
        return wxMaLiveGoodsService.deleteGoods(goodsId);
    }

    /**
     * 审核直播间商品
     *
     * @return auditId 审核id
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String auditGoods(Integer goodsId) {
        WxMaLiveGoodsService wxMaLiveGoodsService = getWxMaLiveGoodsService();
        String auditId = wxMaLiveGoodsService.auditGoods(goodsId);
        return auditId;
    }

}
